#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *

zf9 = lambda x: string.zfill(str(x), 9)
# 2683099 -> 002-683-099
x3x = lambda x: "%s-%s-%s" % (zf9(x)[:3], zf9(x)[3:6], zf9(x)[6:9])

def char_generator(wide=2):
    """ Character generator
    """
    for a in string.letters[:26]:
        for b in string.letters[:26]:
            for c in string.letters[:26]:
                yield string.lower("%s%s%s" % (a,b,c))

#def main(filemask, start='aaa'):
def main(filemask):
    print '''
    add unique id'''

    for f in sorted(glob.glob(filemask)):
        fbare = string.split(f, '\\')[-1]
        #fprep = fbare[:3]
        print '    ', timenow(), fbare
        #ofname = "%s_%s_" % (chid, f)
        ofname = "%s_" % (f)
        of = open(ofname, 'w')
        cd = ''
        lino = 0
        cdcount = {}
        
        # handling file mask as ... f = aa-dataonly\*.dat
        fname = fw_slash(f)
        # fname = aa-dataonly/*.dat
        fname = string.split(fname, '/')[-1]
        # fname = *.dat
        fname = string.split(fname, '.')[0]
        # fname = *
        # take first 3 characters from fname
        fname = fname[:3]
        
        for line in bt.lines(f):
            lino += 1
            if cd == '': cd = findcoldelimiter(line)

            # check columns count
            cdcount[len(string.split(line, cd))] = [lino, line]
            if len(cdcount.keys()) > 1:
                for k in cdcount.keys():
                    print k, cdcount[k]
                sys.exit(2)

            #uid = "*%s*%s" % (fname, x3x(lino)[1:])
            uid = "%s-%s" % (fname, x3x(lino))
            
            of.write("%s%s%s" % (uid, cd, line))
        of.close()
        #chid = iter.next()
        print '    ', timenow()

if __name__ == "__main__":
    """"""
    if len(sys.argv) == 2:
        main(filemask=sys.argv[1])
    else:
        print 'Something is wrong. Try again now.'
